package com.gxsoft.coll.task;

import com.gxsoft.coll.config.ConstantFtpAlcatel;
import com.gxsoft.coll.ftputil.SFTPUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
@Configuration
@EnableScheduling   // 2.开启定时任务
@Slf4j
@SuppressWarnings("all")
public class TransferAlcatel implements SchedulingConfigurer {

    public ConstantFtpAlcatel getConf() {
        return conf;
    }

    @Autowired
    private ConstantFtpAlcatel conf;

    /**
     * 执行定时任务.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        taskRegistrar.addTriggerTask(
                () -> System.out.println("执行动态定时任务start: " + LocalDateTime.now().toLocalTime()),
                triggerContext -> {

//                        setConf(conf);
                    log.info("jobName: "+conf.getJobName());

                    //1.2 从数据库获取执行周期
                    Date nextExecutionTime = new CronTrigger(
                            conf.getJobScanIntervalCron()).nextExecutionTime(triggerContext);

                        log.info("Handle ftp CollAL start..........................");

                        SFTPUtil sftp = null;
                        List<String> filePathList = new ArrayList<String>();
                        try
                        {
                            sftp = new SFTPUtil(conf.getHost(),conf.getUser(),conf.getPass());
                            sftp.connect();
                            // 下载
                            sftp.batchDownLoadFile(conf.getSource(), conf.getTarget(), "tmdw_", null, true,conf.getIntervalMinutes(),conf.getCheckSuccessData());
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        finally
                        {
                            sftp.disconnect();
                        }
                        log.info("Handle order task end..........................");

                    return nextExecutionTime;
                }
        );
    }
    public void setConf(ConstantFtpAlcatel constantFtpAlcatel){

        constantFtpAlcatel.setJobName("Alcatel Transfer Task");
        constantFtpAlcatel.setJobScanIntervalCron("0 */3 * * * ?");
        // 计费系统话单存放目录
        constantFtpAlcatel.setHost("10.100.10.11");
        constantFtpAlcatel.setPort(22);
        constantFtpAlcatel.setUser("billing");
        constantFtpAlcatel.setPass("billing");
        constantFtpAlcatel.setListOpts("");
        constantFtpAlcatel.setListStyle("unix");
        constantFtpAlcatel.setTransferType("I");
        constantFtpAlcatel.setRetries("1");
        constantFtpAlcatel.setOperation("get");
        constantFtpAlcatel.setSource("/app/billing/data/collect/al/");
        constantFtpAlcatel.setTarget("E:\\coll01\\al\\");
        constantFtpAlcatel.setBackup("");
        constantFtpAlcatel.setPost("n");
        constantFtpAlcatel.setLogProcessed("true");
        constantFtpAlcatel.setPostPommand("");
    }
}
